import React, { useState,useRef,useEffect,useMemo } from 'react';
import ProTable, { TableDropdown } from '@ant-design/pro-table';
import { Button, message, Space,Select } from 'antd';
import './css/table.css'
import Popping from './Popping';
import {useSelector,useDispatch,connect} from 'react-redux'

        async function selectPage(params,url) {
            let content;
            let arr = Object.keys(params); 
            if(arr.length > 3){
                url = url + '/get'
                await fetch(url,{
                    method:'post',
                    headers:{
                        'Content-Type':'application/json'
                    },
                    body:JSON.stringify(params)})
                    .then(res=>res.json())
                    .then(res=>{content = res})
                const result = Promise.resolve({
                    data:content,
                    success:true
                });
                return result; 

            }else{
                await fetch(url).then(res=>res.json()).then(res=>{
                    content = res;
                });
                const res = Promise.resolve(
                    {
                        data:content,
                        success:true
                    }
                )
                return res;

            }
            
        }



const Table = ({title,columns,right,url,ques}) => {
    
    const [columnsStateMap, setColumnsStateMap] = useState(right);

    //共享状态
    
    const userList = useSelector(state=> state.user.userList)
    const citculList = useSelector(state=> state.citcul.cityCulutureList)
    const interestList = useSelector(state=> state.interest.interestList)
    const nearbyList = useSelector(state=> state.nearby.nearbyList)
    const foodList = useSelector(state=> state.food.foodList)
    const hotelList = useSelector(state=> state.hotel.hotelList)
    const postList = useSelector(state=> state.post.postList)
    const systemList = useSelector(state=> state.system.systemList)


    return (
        <ProTable columns={columns} 
        params={{sta:{userList,citculList,interestList,nearbyList,foodList,hotelList,postList,systemList}}}
        request={(params) =>selectPage(params,url)}
        options={{

        }} 
        rowKey="key" columnsState={{
            value: columnsStateMap,
            onChange: setColumnsStateMap,
        }} search={
            {
                className:"form",
                labelWidth:100
            }
        } dateFormatter="string" 
        toolBarRender={() => {
            if(title == '系统管理'){
                return [<Button type="primary">无权限</Button>]
            }else if(title == '用户管理'){
                return [<Button type="primary">无权限</Button>]
            }
            else{
                return [
                    <Popping ques={ques} url={url} />
                ]
            }
        }}
        
        tableClassName="table"
        headerTitle={title}
        />
    )
}

export default connect()(Table)
